//package com.example.springsecurity05.config;
//
//import com.example.springsecurity05.service.MyUserDetailService;
//import jakarta.annotation.Resource;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.authentication.AuthenticationManager;
//import org.springframework.security.authentication.BadCredentialsException;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.AuthenticationException;
//import org.springframework.security.core.GrantedAuthority;
//import org.springframework.security.core.userdetails.User;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.crypto.password.PasswordEncoder;
//
//import java.util.Collection;
//
//public class CustomerAuthenticationManager implements AuthenticationManager {
//    @Autowired
//    private MyUserDetailService myUserDetailService;
//
//    @Autowired
//    private PasswordEncoder passwordEncoder;
//
//    @Override
//    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
//        String requestLoginName = authentication.getName();
//        String requestLoginPwd = authentication.getCredentials().toString();
//
//        UserDetails userDetails = myUserDetailService.loadUserByUsername(requestLoginName);
//        User userDetailsDb = (User) userDetails;
//        String passwordFromDb = userDetailsDb.getPassword();
//        String usernameFromDb = userDetailsDb.getUsername();
//        Collection<? extends GrantedAuthority> authorities = userDetails.getAuthorities();
//        if (requestLoginName.equals(usernameFromDb) && passwordEncoder.matches(requestLoginPwd, passwordFromDb)) {
////            return new UsernamePasswordAuthenticationToken(loginRawName, loginRawPwd, authorities);
//            return new UsernamePasswordAuthenticationToken(authentication.getPrincipal(), userDetailsDb, authorities);
//        }else {
//            throw new BadCredentialsException("无效的用户名或密码");
//        }
//    }
//}
